home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14256 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: news2.cais.com!news
  2. From: vanyo@ezaccess.net (Bill Vanyo)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: WEIRD WEIRD Help Please a.s.a.p.:head pointers
  5. Date: Thu, 11 Apr 1996 20:16:25 GMT
  6. Organization: Capital Area Internet Service, Inc.
  7. Message-ID: <4kmh3d$kkd@news2.cais.com>
  8. References: <4km0vb$o04@badger.wmin.ac.uk>
  9. NNTP-Posting-Host: ppp-177.ezaccess.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Idoia Lertxundi <gsoec@wmin.ac.uk> wrote:
  13.  
  14. >Hi C sufferers/lovers,
  15.  
  16. >I am working in the C program and I have come accross a weird prob.
  17. >Rather than sending you code I will explain it in English as I have
  18. >accurately located the problem.
  19.  
  20. The code would have helped enormously.  I feel like I'm geussing
  21. without it, but think I see the problem:
  22.  
  23. >I have a list which is being created dinamycally and a pointer pointing to
  24. >the head of the list. 
  25.  
  26. >ptr=AllocateSpaceforptr();
  27. >head=ptr;
  28.  
  29. > head  ptr....ptr
  30. > |   !   !  |
  31. > {}-{}-{}-NULL
  32.  
  33. >  1  2  3  4
  34.  
  35. >When I have put data in node number 3 I allocate with a malloc call
  36. >node number 4 and as the function do not find suitable data to put 
  37. >in number 4 after a for loop I assign it to NULL. i.e.
  38.  
  39. It would be better not to do that malloc until you know you need it.
  40.  
  41. >     ptr=(FILEMODULE *)NULL;
  42.  
  43. This is not affecting any part of the list pointed to by head.
  44.  
  45. You should be setting the link field of the third node to NULL.
  46.  
  47. My geuss is that your using only the two pointer variables head and
  48. ptr, with head never changing, so that to build the list you're doing
  49. something like
  50.    malloc(ptr->link);
  51.    ptr=ptr->link;
  52. Let's say you just added the fourth node this way.  At this point the
  53. variable ptr and the link field of the third node are both pointing to
  54. the newly allocated fourth node. If you decide you want only four
  55. nodes, do
  56.    ptr->link=NULL;
  57. If, on the other hand, you now decide you want only three nodes, you
  58. need to set the link field of the third node to NULL.  No neat way to
  59. do this.  Doing
  60.    ptr=NULL;
  61. does not affect the link field of the third node -- this still points
  62. to the newly allocated fourth node.
  63.  
  64. Am I geussing right?
  65.  
  66. >When I do printfs I see that the list of ptr corresponds to what I was
  67. >expecting the above picture when I print out the content of the head list
  68. >which points to the begining of the ptr list the result is unexpected.
  69. >i.e.
  70.  
  71. >head  
  72. > |   
  73. > {}-{}-{}-[] the last node instead of being NULL is not defined.
  74.  
  75. >  1  2  3  4
  76.  
  77. >By not defined I mean that is a if after malloc there has not been anything
  78. >put in there although I put the NULL which shows perfectly well in the ptr
  79. >list. Somehow the head list is not receiving the NULL asigment. WHY??
  80.  
  81. >Any ideas you please mail me. I am sending this to a Newsgroup too.
  82.  
  83. >Thank you.
  84.  
  85. >-- 
  86. >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. >Ms Idoia Lertxundi                              .            ,
  88. >                                                            .:/
  89. >e-mail: gsoec@wmin.ac.uk                           .      ,,///;,   ,;/
  90. >URL   : http://www.wmin.ac.uk/~gsoec/                .   o:::::::;;///
  91. >                                                        >::::::::;;\\\
  92. >                                                         `'\\\\\'" `;\
  93. >                                                            `;\
  94. >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. >"All philosophy," I told her, "is based on two things only:
  96. > curiosity and poor eyesight; if you had better eyesight you could see
  97. > perfectly well whether or not these stars are solar systems, and if  
  98. > you were less curious you wouldn't care about knowing, which amounts 
  99. > to the same thing. The trouble is, we want to know more than we can see." 
  100.  
  101. >Bernard de Fontanelle, Conversations on the Plurality of Worlds.
  102.  
  103.  
  104.  
  105.